package parse.impl;

import java.util.ArrayList;
import java.util.HashMap;

import java.util.Map;

import org.dom4j.Document;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import model.ConfigModel;
import model.ReadResultModel;
import parse.ParseCore;

public class ParseToJBO implements ParseCore {

	private static Map<String, ArrayList<ArrayList<String>>> subListMap = null;
	@SuppressWarnings("unchecked")
	private static Map<String, String> convertType = (Map<String, String>) ConfigModel.getConfig("JBOconvertType");
	private static HashMap<String, String> labelMap = new HashMap<>();

	private static int SUBLISTTYPE;

	@Override
	public Object doParse(ReadResultModel readResultModel, String tableName, int subListType) throws Exception {

		// 填充源文件信息
		ArrayList<ArrayList<String>> mainList = readResultModel.getMainList();
		subListMap = readResultModel.getSubListMap();
		SUBLISTTYPE = subListType;

		// 开始构造JBOxml
		Document xmlResult = DocumentHelper.createDocument();
		Element pkg = xmlResult.addElement("package");
		Element classElement = pkg.addElement("class");
		classElement.addAttribute("name", tableName.toLowerCase());
		classElement.addAttribute("label", "");
		classElement.addAttribute("keyAttributes", "serialno");
		Element attributes = classElement.addElement("attributes");
		// 生成主要列表
		attributes.addElement("attribute").addAttribute("name", "serialno").addAttribute("label", "流水号")
				.addAttribute("type", "string").addAttribute("length", "64");
		attributes.addElement("attribute").addAttribute("name", "queryname").addAttribute("label", "业务主键")
				.addAttribute("type", "string").addAttribute("length", "300");
		for (int i = 1; i < mainList.size(); i++) {
			ArrayList<String> record = mainList.get(i);
			String length;
			// 替换类型关键字
			String type = record.get(4);
			// 替换中文括号为英文括号
			type.replace("（", "(");
			type.replace("）", ")");
			int indexOfLeftBracket = type.indexOf("(");
			int indexOfRightBracket = type.indexOf(")");
			String oldType;
			if (indexOfLeftBracket == -1) {
				oldType = type;
				length = "0";
			} else {
				oldType = type.substring(0, indexOfLeftBracket);
				length = type.substring(indexOfLeftBracket + 1, indexOfRightBracket);
			}
			if (convertType.containsKey(oldType.toUpperCase())) {
				record.set(4, convertType.get(oldType.toUpperCase()));
			} else {
				record.set(4, oldType);
			}
			attributes.addElement("attribute").addAttribute("name", record.get(1).toLowerCase())
					.addAttribute("label", record.get(2)).addAttribute("type", record.get(4))
					.addAttribute("length", length);
		}
		attributes.addElement("attribute").addAttribute("name", "edsinputtime").addAttribute("label", "入时间")
				.addAttribute("type", "string").addAttribute("length", "20");
		attributes.addElement("attribute").addAttribute("name", "traceid").addAttribute("label", "业务流水号")
				.addAttribute("type", "string").addAttribute("length", "100");
		Element managerProperties = classElement.addElement("manager").addElement("managerProperties");
		managerProperties.addElement("property").addAttribute("name", "createKey").addAttribute("value", "true");

		for (int i = 1; i < mainList.size(); i++) {
			labelMap.put(mainList.get(i).get(1).toLowerCase(), mainList.get(i).get(2));
		}

		// 生成次要列表
		for (int i = 1; i < mainList.size(); i++) {
			generateSubSQL(pkg, tableName, (mainList.get(i).get(1)));
		}
		return xmlResult;
	}

	private static void generateSubSQL(Element document, String tableName, String subListName) {
		if (subListMap.containsKey(subListName)) {
			ArrayList<ArrayList<String>> subList = subListMap.get(subListName);
			Element classElement = document.addElement("class");
			classElement.addAttribute("name", tableName.toLowerCase() + "_" + subListName);
			if (labelMap.containsKey(subListName.toLowerCase())) {
				classElement.addAttribute("label", labelMap.get(subListName.toLowerCase()));
			} else {
				classElement.addAttribute("label", "");
			}
			classElement.addAttribute("keyAttributes", "serialno");
			Element attributes = classElement.addElement("attributes");
			attributes.addElement("attribute").addAttribute("name", "serialno").addAttribute("label", "流水号")
					.addAttribute("type", "string").addAttribute("length", "64");
			attributes.addElement("attribute").addAttribute("name", "queryname").addAttribute("label", "业务主键")
					.addAttribute("type", "string").addAttribute("length", "300");
			for (int j = 1; j < subList.size(); j++) {
				ArrayList<String> subRecord = subList.get(j);
				String length;
				// 替换类型关键字
				String type = subRecord.get(2);
				// 替换中文括号为英文括号
				type.replace("（", "(");
				type.replace("）", ")");
				int indexOfLeftBracket = type.indexOf("(");
				int indexOfRightBracket = type.indexOf(")");
				String oldType;
				if (indexOfLeftBracket == -1) {
					oldType = type;
					length = "0";
				} else {
					oldType = type.substring(0, indexOfLeftBracket);
					length = type.substring(indexOfLeftBracket + 1, indexOfRightBracket);
				}
				if (1 == SUBLISTTYPE) {
					length = subRecord.get(3);
				}
				if (convertType.containsKey(oldType.toUpperCase())) {
					subRecord.set(2, convertType.get(oldType.toUpperCase()));
				} else {
					subRecord.set(2, oldType);
				}
				attributes.addElement("attribute").addAttribute("name", subRecord.get(0).toLowerCase())
						.addAttribute("label", subRecord.get(1)).addAttribute("type", subRecord.get(2))
						.addAttribute("length", length);
			}
			attributes.addElement("attribute").addAttribute("name", "edsinputtime").addAttribute("label", "入时间")
					.addAttribute("type", "string").addAttribute("length", "20");
			attributes.addElement("attribute").addAttribute("name", "traceid").addAttribute("label", "业务流水号")
					.addAttribute("type", "string").addAttribute("length", "100");
			Element managerProperties = classElement.addElement("manager").addElement("managerProperties");
			managerProperties.addElement("property").addAttribute("name", "createKey").addAttribute("value", "true");
		}
	}

}
